from django.db import models

from apps.member.activity.models import Activity
from apps.member.models import Member
from utils.exceptions import CustomError


class Comment(models.Model):
    member = models.ForeignKey(Member, verbose_name='评论人', on_delete=models.PROTECT)
    activity = models.ForeignKey(Activity, verbose_name='活动', on_delete=models.PROTECT)
    content = models.CharField(verbose_name='内容', max_length=200)
    like_count = models.IntegerField(verbose_name='点赞数', default=0)
    start = models.PositiveSmallIntegerField(verbose_name='评分', default=5)
    delete = models.BooleanField(verbose_name='删除', default=False, editable=False)
    create_time = models.DateTimeField(verbose_name='评论时间', auto_now_add=True)

    class Meta:
        db_table = 'comment'
        verbose_name = '评论'
        ordering = ('-like_count', 'id')

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的id')
        instance = Comment.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的实例')
        return instance


class CommentLike(models.Model):
    comment = models.ForeignKey(Comment, verbose_name='评论', on_delete=models.PROTECT)
    member = models.ForeignKey(Member, verbose_name='用户', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name='点赞时间', auto_now_add=True)

    class Meta:
        db_table = 'comment_like'
        verbose_name = '评论'
